package com.qyhl.webtv.commonlib.utils.view;

import android.content.Context;
import android.graphics.Rect;
import android.os.Build;
import androidx.annotation.Nullable;
import android.util.AttributeSet;
import android.view.WindowInsets;
import android.widget.LinearLayout;

/**
 * Created by qyhl on 2018/4/23.
 * 自适应全屏线性布局
 * 解决设置状态栏FLAG_TRANSLUCENT_STATUS 后与输入框adjustResize冲突
 * 在xml中设置fitSystemWindows =true
 *
 * @author qyhl
 */

public class FitFullScreenLinearLayout extends LinearLayout {
    private int[] mInsets = new int[4];

    public FitFullScreenLinearLayout(Context context) {
        super(context);
    }

    public FitFullScreenLinearLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public FitFullScreenLinearLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public final int[] getInsets() {
        return mInsets;
    }

    @Override
    protected final boolean fitSystemWindows(Rect insets) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
            mInsets[0] = insets.left;
            mInsets[1] = insets.top;
            mInsets[2] = insets.right;
            return super.fitSystemWindows(insets);
        } else {
            return super.fitSystemWindows(insets);
        }
    }

    /*
     *此方法以过期，当应用最低API支持为20后，可以重写以下方法
     */
    @Override
    public final WindowInsets onApplyWindowInsets(WindowInsets insets) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
            mInsets[0] = insets.getSystemWindowInsetLeft();
            mInsets[1] = insets.getSystemWindowInsetTop();
            mInsets[2] = insets.getSystemWindowInsetRight();
            return super.onApplyWindowInsets(insets.replaceSystemWindowInsets(0, 0, 0,
                    insets.getSystemWindowInsetBottom()));
        } else {
            return insets;
        }
    }
}
